#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import datetime
import time
import re

import krakenex

showTrue=1
showFalse=0

kraConn = krakenex.API()
kraConn.load_key('kraken.key')

def balanceKRA(show):
    KRA_balance = kraConn.query_private('Balance')
    ktoEUR = KRA_balance['result']['ZEUR']
    ktoBTC = KRA_balance['result']['XXBT']
    ktoLTC = KRA_balance['result']['XLTC']
    if show:
        print "Balance KRA"
        print "BTC=",ktoBTC, "LTC=",ktoLTC, "EUR=",ktoEUR
    return {u'BTC':float(ktoBTC),u'LTC':float(ktoLTC),u'EUR':float(ktoEUR)}
    
def getKraTicker(show):
    kraBitcoin2ltc = kraConn.query_public('Ticker', {'pair':'XXBTXLTC'})
    kraBitcoin2eur = kraConn.query_public('Ticker', {'pair':'XXBTZEUR'})
    if show:
        print "KRA sells 1 BTC for",kraBitcoin2eur['result']['XXBTZEUR']['a'][0], "EUR"
        print "KRA buys  1 BTC for",kraBitcoin2eur['result']['XXBTZEUR']['b'][0], "EUR"
    return {u'Sells':float(kraBitcoin2eur['result']['XXBTZEUR']['a'][0]),u'Buys':float(kraBitcoin2eur['result']['XXBTZEUR']['b'][0])}

def getKraOpenOrders(show):
    openOrdersCount=0    
    try:
        openOrders = kraConn.query_private('OpenOrders', {'pair':'XXBTXLTC'})
        for key in openOrders['result']['open'].keys():                
                type =openOrders['result']['open'][key]['descr']['type']
                price=openOrders['result']['open'][key]['descr']['price']
                volBTC=openOrders['result']['open'][key]['vol']
                order=openOrders['result']['open'][key]['descr']['order']
                openOrdersCount=openOrdersCount+1
                if show:
                    print type, price, volBTC, order
    except:
        print "no open Orders"
    return [openOrdersCount,openOrders]        

def balanceCex(show):
    bal = cexConn.balance()    
    if show:
        print "CexKto BTC=",bal['BTC']['available'], "LTC=",bal['LTC']['available']    
    return {u'BTC':float(bal['BTC']['available']),u'LTC':float(bal['LTC']['available'])} 
     
def getCexTicker(show): 
    order_bookLTC = cexConn.order_book("LTC/BTC") 
    ltc_bestBid=float(order_bookLTC['bids'][0][0])  
    ltc_bestAsk=float(order_bookLTC['asks'][0][0])           
    ltcBestBuyPrice=1/ltc_bestAsk
    ltcBestSellPrice=1/ltc_bestBid
    if show:
        print "CEX sells 1 BTC for",ltcBestSellPrice, "\tLTC -> buy LTC @",1/ltcBestSellPrice
        print "CEX buys  1 BTC for",ltcBestBuyPrice, "\tLTC-> sell LTC @",1/ltcBestBuyPrice
    return {u'Sells':ltcBestSellPrice,u'Buys':ltcBestBuyPrice}

def getCexOpenOrders(show):
    openOrdersCount=0
    currOrders = cexConn.current_orders('LTC/BTC')
    sortedCurrOrders = sorted(currOrders, key=lambda order: order['price'], reverse=True)    
    for order in sortedCurrOrders:
        openOrdersCount=openOrdersCount+1
        if show:
            print order
    return [openOrdersCount,sortedCurrOrders]        


counter=100
btc2Change=0.4279

while True and counter>0:
    counter=counter-1
    
    print "Status"       
    print "KRA Balance/Ticker"
    balKra=balanceKRA(show=True)
    tickerKra=getKraTicker(showTrue)
    print "btc2Change=",btc2Change, "is worth",round(tickerKra['Buys']*btc2Change,2),"EUR"    
               
    time.sleep(10)